package org.firebug.spring.boot.ssj.service;

import org.firebug.common.bean.api.Pagination;
import org.firebug.spring.boot.ssj.repository.dao.UserDAO;
import org.firebug.spring.boot.ssj.repository.po.UserPO;
import org.firebug.spring.boot.ssj.repository.qo.UserQO;
import org.firebug.spring.boot.ssj.tools.BeanTools;
import org.firebug.spring.boot.ssj.web.vo.UserVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserService {
    @Resource
    private UserDAO userDAO;

    public void save(UserVO vo) {
        UserPO po = new UserPO();
        po.setName(vo.getName());
        userDAO.insertAutoIncrement(po);
    }

    public void deletePhysicalById(long id) {
        userDAO.deletePhysicalById(id);
    }

    public void updateById(UserVO vo) {
        UserPO po = BeanTools.convertBean(vo, UserPO.class);
        userDAO.updateById(po);
    }

    public List<UserVO> selectById(long id) {
        List<UserPO> pos = userDAO.selectById(id);
        return BeanTools.convertList(pos, UserVO.class);
    }

    public List<UserVO> selectByQO(UserQO qo, Pagination pagination) {
        pagination.setRct(userDAO.selectCountByQO(qo));
        List<UserPO> pos = userDAO.selectByQO(qo, pagination.offset(), pagination.limit());
        return BeanTools.convertList(pos, UserVO.class);
    }
}
